package com.example.monitoringsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.monitoringsystem.bean.Doctor;
import com.example.monitoringsystem.bean.Patient;
import com.example.monitoringsystem.bean.User;
import com.example.monitoringsystem.mapper.DoctorMapper;
import com.example.monitoringsystem.mapper.PatientMapper;
import com.example.monitoringsystem.mapper.UserMapper;
import com.example.monitoringsystem.service.PatientService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean chooseDoctorByDoctorId(int doctorId) {
        try{
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        int patientId=currentUser.getId();
        UpdateWrapper<Patient> wrapper = new UpdateWrapper<>();
        wrapper.eq("patient_id",patientId).set("doctor",doctorId);
        patientMapper.update(null,wrapper);
        return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Map getDoctorByDoctorId(int doctorId) {
        Map<String,Object> map=new HashMap<>();
        //根据医生id查询医生信息
        QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("doctor_id",doctorId);
        Doctor doctor = doctorMapper.selectOne(wrapper2);
        map.put("doctor",doctor);

        //根据医生id查询用户信息
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id",doctorId);
        User user = userMapper.selectOne(wrapper3);
        map.put("user",user);
        return map;
    }


    @Override
    public Map getDoctorByPatientId(int patientId) {
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        wrapper.select("doctor").eq("patient_id",patientId);
        List<Object> list = patientMapper.selectObjs(wrapper);
        int doctorId=(int)list.get(0);//得到病人对应的医生id

        //根据医生id查询医生信息
        QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("doctor_id",doctorId);
        Doctor doctor = doctorMapper.selectOne(wrapper2);
        map.put("doctor",doctor);

        //根据医生id查询用户信息
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id",doctorId);
        User user = userMapper.selectOne(wrapper3);
        map.put("user",user);
        return map;
    }

    @Override
    public List<User> getAllDoctorUsers() {
        QueryWrapper<User> wrapper1=new QueryWrapper<>();
        wrapper1.eq("role","patient");
        return userMapper.selectList(wrapper1);
    }

    @Override
    public List<Doctor> getAllDoctors() {
        QueryWrapper<Doctor> wrapper=new QueryWrapper<>();
        List<Doctor> list = doctorMapper.selectList(wrapper);
        return list;
    }

}
